package com.ruoyi.report.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.oiltank.domain.OilTank;
import com.ruoyi.oiltank.service.IOilTankService;
import com.ruoyi.report.domain.ReportOilDepotDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.report.domain.ReportDcDetail;
import com.ruoyi.report.mapper.ReportDcMapper;
import com.ruoyi.report.domain.ReportDc;
import com.ruoyi.report.service.IReportDcService;

/**
 * 配送中心Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-15
 */
@Service
public class ReportDcServiceImpl implements IReportDcService 
{
    @Autowired
    private ReportDcMapper reportDcMapper;
    @Autowired
    private IOilTankService oilTankService;
    /**
     * 查询配送中心
     * 
     * @param id 配送中心ID
     * @return 配送中心
     */
    @Override
    public ReportDc selectReportDcById(Long id)
    {
        return reportDcMapper.selectReportDcById(id);
    }

    /**
     * 查询配送中心列表
     * 
     * @param reportDc 配送中心
     * @return 配送中心
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<ReportDc> selectReportDcList(ReportDc reportDc)
    {
        List<String> dateQuery = reportDc.getDateQuery();
        if (CollectionUtil.isNotEmpty(dateQuery)){
            String start = dateQuery.get(0);
            String end = dateQuery.get(1);
            reportDc.setStart(start);
            reportDc.setEnd(end);
        }
        return reportDcMapper.selectReportDcList(reportDc);
    }

    /**
     * 新增配送中心
     * 
     * @param reportDc 配送中心
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReportDc(ReportDc reportDc)
    {
        reportDc.setCreateTime(DateUtils.getNowDate());
        int rows = reportDcMapper.insertReportDc(reportDc);
        insertReportDcDetail(reportDc);

        List<ReportDcDetail> reportDcDetailList = reportDc.getReportDcDetailList();
        for (ReportDcDetail reportDcDetail : reportDcDetailList) {
            Long tankId = reportDcDetail.getTankId();
            BigDecimal tankHeight = reportDcDetail.getTankHeight();
            Integer tankStatus = reportDcDetail.getTankStatus();
            BigDecimal tankWeight = reportDcDetail.getTankWeight();
            OilTank oilTank = oilTankService.selectOilTankById(tankId);
            if (ObjectUtil.isNull(oilTank)){
                throw new RuntimeException("查询不到储罐信息");
            }
            oilTank.setId(tankId);
            oilTank.setStatus(tankStatus);
            oilTank.setWeight(tankWeight);
            oilTank.setHeight(tankHeight);
            oilTankService.updateOilTank(oilTank);

        }

        return rows;
    }

    @Override
    public Integer countTodayRecordsDc(Long deptId) {
     return reportDcMapper.countTodayRecordsDc(deptId);
    }

    /**
     * 修改配送中心
     * 
     * @param reportDc 配送中心
     * @return 结果
     */
    @Transactional
    @Override
    public int updateReportDc(ReportDc reportDc)
    {
        reportDc.setUpdateTime(DateUtils.getNowDate());
        reportDcMapper.deleteReportDcDetailByDcId(reportDc.getId());
        insertReportDcDetail(reportDc);


        List<ReportDcDetail> reportDcDetailList = reportDc.getReportDcDetailList();
        for (ReportDcDetail reportDcDetail : reportDcDetailList) {
            Long tankId = reportDcDetail.getTankId();
            BigDecimal tankHeight = reportDcDetail.getTankHeight();
            Integer tankStatus = reportDcDetail.getTankStatus();
            BigDecimal tankWeight = reportDcDetail.getTankWeight();
            OilTank oilTank = oilTankService.selectOilTankById(tankId);
            if (ObjectUtil.isNull(oilTank)){
                throw new RuntimeException("查询不到储罐信息");
            }
            oilTank.setId(tankId);
            oilTank.setStatus(tankStatus);
            oilTank.setWeight(tankWeight);
            oilTank.setHeight(tankHeight);
            oilTankService.updateOilTank(oilTank);

        }


        return reportDcMapper.updateReportDc(reportDc);
    }

    /**
     * 批量删除配送中心
     * 
     * @param ids 需要删除的配送中心ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReportDcByIds(Long[] ids)
    {
        reportDcMapper.deleteReportDcDetailByDcIds(ids);
        return reportDcMapper.deleteReportDcByIds(ids);
    }

    /**
     * 删除配送中心信息
     * 
     * @param id 配送中心ID
     * @return 结果
     */
    @Override
    public int deleteReportDcById(Long id)
    {
        reportDcMapper.deleteReportDcDetailByDcId(id);
        return reportDcMapper.deleteReportDcById(id);
    }

    /**
     * 新增航油配送中心信息
     * 
     * @param reportDc 配送中心对象
     */
    public void insertReportDcDetail(ReportDc reportDc)
    {
        List<ReportDcDetail> reportDcDetailList = reportDc.getReportDcDetailList();
        Long id = reportDc.getId();
        if (StringUtils.isNotNull(reportDcDetailList))
        {
            List<ReportDcDetail> list = new ArrayList<ReportDcDetail>();
            for (ReportDcDetail reportDcDetail : reportDcDetailList)
            {
                reportDcDetail.setDcId(id);
                list.add(reportDcDetail);
            }
            if (list.size() > 0)
            {
                reportDcMapper.batchReportDcDetail(list);
            }
        }
    }
}
